<template>
	<div>
    <zh-form ref="excelModelFormRef"  :model="state.form"  :rules="state.rules">
      <zh-form-input
				prop="id"
        :disabled="true"
				label="ID"
				placeholder=""
				v-model="state.form.id"
        hidden="true"
			></zh-form-input>

      <zh-form-input
				prop="projectId"
        :disabled="props.showFlag"
				label="所属项目"
				placeholder=""
				v-model="state.form.projectId"
			></zh-form-input>


        <zh-form-select
            prop="tableId"
            label="表名"
            placeholder="单选"
            allowClear
            :options="state.tableApiDict"
            :fieldNames="{ label: 'tableFullName', value: 'id' }"
            v-model="state.form.tableId"
        ></zh-form-select>

        <zh-form-input prop="columnName" label="字段名" placeholder=""
                       v-model="state.form.columnName"></zh-form-input>
        <zh-form-input prop="columnComment" label="字段中文名" placeholder=""
                       v-model="state.form.columnComment"></zh-form-input>
        <zh-form-input prop="columnDesc" label="字段备注" placeholder=""
                       v-model="state.form.columnDesc"></zh-form-input>

        <zh-form-select
            prop="dataType"
            label="字段类型"
            :options="state.dictData.fieldType"
            placeholder="请选择"
            v-model="state.form.dataType"
        ></zh-form-select>

        <zh-form-input prop="dataLength" label="字段长度" placeholder=""  type="number"
                       v-model.number="state.form.dataLength"></zh-form-input>
        <zh-form-input prop="dataLength" label="精度" placeholder=""  type="number"
                       v-model.number="state.form.dataLength"></zh-form-input>
        <zh-form-input prop="dataScale" label="小数位数"  placeholder="" type="number"
                       v-model.number="state.form.dataScale"></zh-form-input>
        <zh-form-select
            prop="primaryKey"
            label="主键标志"
            :options="state.dictData.primaryKey"
            placeholder="请选择"
            v-model="state.form.primaryKey"
        ></zh-form-select>
        <zh-form-select
            prop="nullable"
            label="是否为空"
            :options="state.dictData.nullable"
            placeholder="请选择"
            v-model="state.form.nullable"
        ></zh-form-select>
      <zh-form-input prop="columnDefault" label="字段默认值" placeholder=""
                     v-model.number="state.form.columnDefault"></zh-form-input>
        <zh-form-select
            prop="safelevel"
            label="字段分级"
            :options="state.dictData.safelevel"
            placeholder="请选择"
            v-model="state.form.safeLevel"
        ></zh-form-select>
        <zh-form-select
            prop="desensitization"
            label="脱敏类型"
            :options="state.dictData.desensitization"
            placeholder=""
            v-model="state.form.desensitization"
        ></zh-form-select>
        <zh-form-select
            prop="encryption"
            label="是否加密"
            :options="state.dictData.encryption"
            placeholder="请选择"
            v-model="state.form.encryption"
        ></zh-form-select>




    </zh-form>
  </div>
</template>
<script lang="ts" setup name="ExcelModelForm">
import { ElMessage } from 'element-plus';
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	modelInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
  showFlag:{
    type:Boolean,
    default:false
  },
  modelValue: Boolean,

});
//------------------定义子组件---------------------------
const excelModelFormRef=ref()
const emit = defineEmits(['update:modelValue']);
//------------------内部变量定义-------------------------
const state=reactive({
  form:{newFlag:'0'} as any,
  rules:{
    columnName: [
      {
        required: true,
        message: '请输入字段名',
        trigger: 'blur',
      },
    ],

  },
  dictData: {} as any,
  apiDict:[],
  tableApiDict:[],

})
//------------------定义调用方法-------------------------

function getDetail(){
  if(!props.modelInfo||!props.modelInfo.id){
    return;
  }
  let params={id:props.modelInfo.id};
  $api.common.send.post('coTableFieldMeta0002', params).then((res) => {
    // state.codeDisabled=true
		state.form = Object.assign({},res.result);
	});
}

function addInit(){
    if(!props.modelInfo||!props.modelInfo.tableId){
        return;
    }
    state.form.tableId = props.modelInfo.tableId;

}

async function save(){
    console.log('修改方法');
  //校验表单
	let checkFlag = await excelModelFormRef.value.checkForm();
	if (checkFlag) {
    const info = state.form;
    let ret=' ' as any;
	if (!props.showFlag&&info.id) {
      ret=await $api.common.send.post('coTableFieldMeta0005',info);
      ret.saveType = 'edit';
      if(ret.code === 0){
        ElMessage.success('修改成功！');
        emit('update:modelValue', false);
        console.log('修改成功 = ');

      }
    } else {
      ret=await $api.common.send.post('coTableFieldMeta0004',info);
      ret.saveType = 'add';
      if(ret.code === 0){
        ElMessage.success('新建成功！');
        emit('update:modelValue', false);
        console.log('新增成功 = ');
      }
		}
    return ret;
  }
	return null;
}
//------------------初始化------------------------------
onMounted(()=>{
  addInit();
  getDetail();
})

const params = {
    dictFields: 'fieldType,primaryKey,nullable,safelevel,desensitization,encryption',
  };
  $api.common.getBatchDict.post(params).then((res) => {
    state.dictData = res.result;
  });

  $api.common.send.post('dataSource0001',{}).then((res) => {
  state.apiDict = res.result.list;
  });

  $api.common.send.post('coTableMeta0001',{}).then((res) => {
  state.tableApiDict = res.result.list;
  });

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form ,save});
</script>
<style lang="scss" scoped></style>
